<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="elderName">
        <el-input
          v-model="queryParams.elderName"
          placeholder="请输入老人的姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入住日期" prop="checkInDate">
        <el-date-picker clearable
          v-model="queryParams.checkInDate"
          type="date"
          value-format="timestamp"
          placeholder="请选择入住日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="房间号码" prop="roomNumber">
        <el-input
          v-model="queryParams.roomNumber"
          placeholder="请输入房间号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index"  width="50"/>
      <el-table-column label="姓名" align="center" prop="elderName" />
      <el-table-column label="年龄" align="center" prop="elderAge" />
      <el-table-column label="性别" align="center" prop="elderGender" />
      <el-table-column label="出生日期" align="center" prop="birthDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入住日期" align="center" prop="checkInDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkInDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退住日期" align="center" prop="checkOutDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkOutDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="房间号码" align="center" prop="roomNumber" />
      <el-table-column label="床位号码" align="center" prop="bedNumber" />
      <el-table-column label="押金金额" align="center" prop="deposit" />
      <el-table-column label="入住状态" align="center" prop="liveStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDetails(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公寓" prop="apartmentId">
          <el-select clearable v-model="form.apartmentId" placeholder="请选择公寓名称">
            <el-option v-for="option in apartmentList" :key="option.apartmentId" :label="option.apartmentName" :value="option.apartmentId" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="elderName">
          <el-input v-model="form.elderName" placeholder="请输入老人的姓名" />
        </el-form-item>
        <el-form-item label="年龄" prop="elderAge">
          <el-input v-model="form.elderAge" placeholder="请输入老人的年龄" />
        </el-form-item>
        <el-form-item label="性别" prop="elderGender">
          <el-input v-model="form.elderGender" placeholder="请输入老人的性别" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker clearable
            v-model="form.birthDate"
            type="date"
            value-format="timestamp"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入住日期" prop="checkInDate">
          <el-date-picker clearable
            v-model="form.checkInDate"
            type="date"
            value-format="timestamp"
            placeholder="入住日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退住日期" prop="checkOutDate">
          <el-date-picker clearable
            v-model="form.checkOutDate"
            type="date"
            value-format="timestamp"
            placeholder="请选择老人离开公寓的日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="房间号码" prop="roomNumber">
          <el-input v-model="form.roomNumber" placeholder="请输入房间号码" />
        </el-form-item>
        <el-form-item label="床位号码" prop="bedNumber">
          <el-input v-model="form.bedNumber" placeholder="请输入床位号码" />
        </el-form-item>
        <el-form-item label="身份证号码" prop="identityCardNumber">
          <el-input v-model="form.identityCardNumber" placeholder="请输入身份证号码" />
        </el-form-item>
        <el-form-item label="联系电话号码" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="联系电话号码" />
        </el-form-item>
        <el-form-item label="老人住址信息" prop="homeAddress">
          <el-input v-model="form.homeAddress" placeholder="请输入住址信息" />
        </el-form-item>
        <el-form-item label="手术史的信息" prop="hasSurgeryHistory">
          <el-input v-model="form.hasSurgeryHistory" placeholder="请输入手术史的信息" />
        </el-form-item>
        <el-form-item label="慢性疾病的信息" prop="hasChronicDisease">
          <el-input v-model="form.hasChronicDisease" placeholder="请输入慢性疾病的信息" />
        </el-form-item>
        <el-form-item label="精神疾病的信息" prop="hasMentalIllness">
          <el-input v-model="form.hasMentalIllness" placeholder="请输入精神疾病的信息" />
        </el-form-item>
        <el-form-item label="老人传染性疾病的信息" prop="hasInfectiousDisease">
          <el-input v-model="form.hasInfectiousDisease" placeholder="请输入传染性疾病的信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="isDetails">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/business/info";
import {listElderlyapartment} from "@/api/business/elderlyapartment";
export default {
  name: "Info",
  data() {
    return {
      //是不是新增
      isAdd: true,
      //是否为老人详情
      isDetails: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //公寓信息
      apartmentList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        elderId: null,
        elderName: null,
        elderAge: null,
        elderGender: null,
        birthDate: null,
        checkInDate: null,
        checkOutDate: null,
        roomNumber: null,
        bedNumber: null,
        liveStatus: null,
        identityCardNumber: null,
        contactPhone: null,
        homeAddress: null,
        healthStatus: null,
        hasSurgeryHistory: null,
        hasChronicDisease: null,
        hasMentalIllness: null,
        hasInfectiousDisease: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //获取公寓信息
    getApartmentInfo(){
      listElderlyapartment(this,this.queryParams).then(response =>{
        this.apartmentList = response.result;
        this.total = response.total;
        this.loading = false;
      })
    },
    /** 查询老人信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        elderId: null,
        elderName: null,
        elderAge: null,
        elderGender: null,
        birthDate: null,
        checkInDate: null,
        checkOutDate: null,
        roomNumber: null,
        bedNumber: null,
        liveStatus: null,
        identityCardNumber: null,
        contactPhone: null,
        homeAddress: null,
        healthStatus: null,
        hasSurgeryHistory: null,
        hasChronicDisease: null,
        hasMentalIllness: null,
        hasInfectiousDisease: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    handleDetails(row){
      this.reset();
      const id = row.id;
      getInfo(id).then(response =>{
        this.form = response.data;
        this.open = true;
        this.title = "老人详情";
        this.isDetails = false;
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.createUserId = 
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
